from src.common.base_driver import BaseDriver, create_driver


class BaseApp:
    appium_server_address = "http://127.0.0.1:4723/wd/hub"

    def __init__(self, device_name: str, app_package: str, app_start_activity: str, driver: BaseDriver = None):
        self.device_name = device_name
        self.app_package = app_package
        self.app_start_activity = app_start_activity
        self.driver = driver

        # capability启动配置参数
        self.caps = {
            "automationName": "uiautomator2",
            "platformName": "android",
            "appium:platformVersion": "9",
            # appium请求等待最长时间
            "appium:newCommandTimeout": 3600,
            # 设置idle状态的超时时间
            "settings[waitForIdleTimeout]": 0,
            # 不清空缓存启动app
            "appium:noReset": True,
            "appium:dontStopAppOnReset": True,
            "appium:deviceName": device_name,
            "appium:appPackage": app_package,
            "appium:appActivity": app_start_activity
        }

    def start(self):
        """
        启动app
        """
        # 连接appium服务器
        self.driver = create_driver(self.appium_server_address, self.caps)
        from appium.common.logger import logger
        logger.info(self.device_name + "启动成功！")

    def quit(self):
        """
        关闭app释放Driver资源
        """
        self.driver.quit()
